﻿<?php
/**
* @version		$Id: szaki_gallery.php 2009. 03. 18.
* @package		Joomla
* @copyright	Copyright (C) 2009 szathmari.hu All rights reserved.
* @license		GNU/GPL, see license on http://szathmari.hu
*/
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.event.plugin' );

class plgContentszaki_gallery extends JPlugin
{
	var $parameter;
	public $var = 1;

	function createthumb( $i, $w, $h, $q, $m, $tf )
	{
		$thumb = new Thumbnail( $i );
		$thumb->resize( $w, $h );
		if ($m)
			$thumb->createReflection( 40, 40, 80, false, '#a4a4a4' );
		$thumb->save( $tf, $q );
	}

	function directory( $dir, $filter )
	{
		$dir_ab = $_SERVER['DOCUMENT_ROOT'];
		$handle = opendir( $dir_ab . '/' . $dir );
		$files = array();
		while (($file = readdir( $handle )) !== false)
		{
			$system = explode( ".", $file );
			$path = pathinfo( $file );
			if (preg_match( $filter, $path['extension'] ))
			{
				$f = $path['filename'] . '_tmb.jpg';
				if (!preg_match( '/_tmb$/i', $path['filename'] ))
				{
					$files['img'][] = $dir . $file;
					$files['tmb'][] = $f;
					if (!file_exists( $dir_ab . '/' . $dir . '/' . $f ))
					{
						$this->createthumb( $dir_ab . '/' . $dir . '/' . $file, 340, 220, 85, 0, $dir_ab . '/' . $dir . '/' . $f );
					}
				}
			}
		}
		closedir( $handle );
		return $files;
	}

	function onAfterDisplayContent( &$article, &$params, $limitstart )
	{
		global $mainframe;
		$plugin = &JPluginHelper::getPlugin( 'content', 'szaki_gallery' );
		$pluginParams = new JParameter( $plugin->params );
		JPlugin::loadLanguage( 'plg_content_szaki_gallery' );
		$this->szaki_gallery_PrepareSettings( $pluginParams );
		$this->width = $pluginParams->get( 'width', 150 );
		$this->height = $pluginParams->get( 'height', 150 );
		$this->style = $pluginParams->get( 'stlye', '1' );
		$regex = "#{sg\s*(.*?)}#s";
		$article->text = preg_replace_callback( $regex, array($this, 'SetParameters'), $article->text );
	}

	function szaki_gallery_PrepareSettings( $pluginParams )
	{
		global $mainframe;
		$document = &JFactory::getDocument( );
		$plugin_base = 'plugins/content/szaki_gallery/';
		if ($pluginParams->get( 'stlye', '1' ) == 1)
		{
			$document->addStyleSheet( $plugin_base . 'szaki_gallery.css' );
		}
		else
		{
			$document->addStyleSheet( $plugin_base . 'szaki_gallery2.css' );
		}
		JHTML::script( 'jquery.js', $plugin_base );
		JHTML::script( 'jquery.galleria.pack.js', $plugin_base );
	}

	function SetParameters( &$content )
	{
		if ($this->var > 1)
			return '<b><i>' . JText::_( 'Oldalanként egy galéria jeleníthető meg!' ) . '</i></b>';
		$this->parameter = array();
		preg_match_all( "#(\w+)\s*=\s*\"\s*(.*?)\s*\"#s", $content[1], $matches );
		for ($i = 0; $i < count( $matches[1] ); $i++)
		{
			$this->parameter[strtolower( $matches[1][$i] )] = $matches[2][$i];
		}
		if (!$this->parameter['src'] && !is_dir( $this->parameter['src'] ))
		{
			return;
		}
		$document = &JFactory::getDocument( );
		$base = JURI::getInstance( JURI::root( ))->getPath( );
		$imgdir = $this->parameter['src'];
		include_once ('szaki_gallery/thumbnail.inc.php');
		$pics = $this->directory( $imgdir, "/\bjpg\b|\bjpeg\b/i" );
		$div = "<div class='sgallerydiv'><div id='main_image'></div>\n";
		$div .= "<ul class='sgallery_unstyled'>";
		$i = 0;
		foreach ($pics['img'] as $image)
		{
			$div .= "<li><img src='" . $imgdir . '/' . $pics['tmb'][$i] . "' alt='" . $this->parameter['alt' . ($i + 1)] . "' title='" . $this->parameter['title' . ($i + 1)] . "'></li>";
			$i++;
		}
		$div .= '</ul>';
		$div .= "<div class=\"sgallerynav\"><a href=\"#\" onclick=\"\$j.galleria.prev(); return false;\">" . JText::_( 'prev' ) . "</a> | <a href=\"#\" onclick=\"\$j.galleria.next(); return false;\">" . JText::_( 'next' ) . "</a></div>";
		$div .= '</div>';
		if ($this->var == 1)
			$jscript1 = "var \$j = jQuery.noConflict()";
		$jscript1 .= "
        \$j(document).ready(function(){
        \$j('.sgallery_unstyled').addClass('sgallery'); 

        \$j('ul.sgallery').galleria({
        history   : true,
        clickNext : true,
        insert    : '#main_image',
        onImage   : function(image,caption,thumb) {

				image.css('display','none').fadeIn(1000);
				caption.css('display','none').fadeIn(1000);

				var _li = thumb.parents('li');

				_li.siblings().children('img.selected').fadeTo(500,0.3);
				thumb.fadeTo('fast',1).addClass('selected');
				image.attr('title','Következő >>');

			},
			onThumb : function(thumb) {
				var _li = thumb.parents('li');
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); }
				)
			}
		});
	});
        ";
		$jscript2 = "var \$j = jQuery.noConflict();
        \$j(document).ready(function(){
        \$j('.sgallery_unstyled').addClass('sgallery');
        \$j('.sgallerynav').css('display','none');

        \$j('ul.sgallery').galleria({
        history   : false,
        clickNext : false,
        insert    : undefined,
        onImage   : function() { \$j('.sgallerynav').css('display','inline'); }
        });
        });";
		if ($this->style == 1)
		{
			$document->addScriptDeclaration( $jscript1 );
		}
		else
			$document->addScriptDeclaration( $jscript2 );
		$scriptA = "\$j(document).ready(function(){";
		$scriptA .= "\$j.galleria.activate('" . $imgdir . '/' . $pics['tmb'][0] . "');";
		$scriptA .= '});';
		$document->addScriptDeclaration( $scriptA );
		$this->var++;
		return $div;
	}
}
?>
